package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.place.entity.apiresp.RespCityInfos;
import com.quanyan.place.entity.apiresp.RespDistrict;
import com.quanyan.stadium.api.req.ReqId;
import com.quanyan.stadium.api.req.ReqIds;
import com.quanyan.stadium.entity.vo.PlaceInfo;
import com.quanyan.stadium.service.commonComponent.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RestController("apiGameBase")
@RequestMapping("/api/stadium/base")
public class BaseController {

    @Autowired
    private BaseService baseService;

    /**
     *  获取运动场默认配置
     * */
    @RequestMapping(value="/getStadiumDefaultConfig", method = RequestMethod.POST)
    public APIResponse<?> getStadiumDefaultConfig() throws InvocationTargetException, IllegalAccessException {
        return APIResponse.returnSuccess(baseService.getStadiumDefaultConfig());

    }

    /**
     *  获取比赛的banner图
     * */
    @RequestMapping(value="/gameBanner", method = RequestMethod.POST)
    public APIResponse<?> gameBanner(@RequestBody ReqId reqId) throws InvocationTargetException, IllegalAccessException {
        if (null == reqId.getCityId() || null ==reqId.getDistrictId()) {
            return APIResponse.returnFail("参数问题");
        }
        return baseService.gameBanner(reqId.getCityId(), reqId.getDistrictId());
    }


    /**
     * 获取城市列表
     *
     * */
    @RequestMapping(value="/queryUsableCity", method = RequestMethod.POST)
    public APIResponse<?> queryUsableCity()  {
        List<RespCityInfos> msg = baseService.queryRespCityInfos();
        return APIResponse.returnSuccess(msg);
    }


    /**
     * 城市下的区域列表
     *
     * */
    @RequestMapping(value="/queryDistrictListByCityId", method = RequestMethod.POST)
    public APIResponse<?> queryDistrictListByCityId(@RequestBody ReqId reqId) throws InvocationTargetException, IllegalAccessException {
        if (null == reqId.getCityId()) {
            return APIResponse.returnFail("cityId不能为空");
        }
        List<RespDistrict> msg =  baseService.queryDistrictListByCityId(reqId.getCityId());
        return APIResponse.returnSuccess(msg);
    }


    /**
     * 获取某城市下的场馆列表
     *
     * */
    @RequestMapping(value="/queryPlaceInfoByDistrictId", method = RequestMethod.POST)
    public APIResponse<?> queryPlaceInfoByDistrictId(@RequestBody ReqId reqId, BindingResult result)  {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getDistrictId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getCityId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        List<PlaceInfo> msg = baseService.queryPlaceInfoByDistrictId(reqId.getCityId(),reqId.getDistrictId(),reqId.getCategoryId());
        PlaceInfo placeInfo = new PlaceInfo();
        placeInfo.setPlaceId(0);
        placeInfo.setName("全部场馆");
        msg.add(0, placeInfo);
        return APIResponse.returnSuccess(msg);
    }

    /**
     * 获取当前场馆的可用场地
     *
     * */
    @RequestMapping(value="/queryUsablePlaceUnits", method = RequestMethod.POST)
    public APIResponse<?> queryUsablePlaceUnits(@RequestBody ReqId reqId)  {
        return baseService.queryUsablePlaceUnits(reqId.getPlaceId());
    }

    /**
     *  根据场馆id集合获取场馆的基本对象集合
     */
    @RequestMapping(value = "/queryPlaceInfoMsgList",method = RequestMethod.POST)
    public APIResponse<?> queryPlaceInfoMsgList(@RequestBody ReqIds ids){
        return APIResponse.returnSuccess(baseService.queryPlaceInfoMsgList(ids.getIds()));
    }
}
